/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sniper.config;

import com.sniper.utils.PropertiesUtil;
import com.sniper.utils.ValidateUtil;

import java.io.InputStream;
import java.util.*;

/**
 * 全局配置类
 *
 * @author ThinkGem
 * @version 2013-03-23
 */

public class Global {

    private static final String APPLICATION_CONFIG = "properties/application..properties";

    /**
     * 保存全局属性值
     */
    private static Map<String, String> tempMap = new LinkedHashMap<>();

    static {
        init();
    }

    public static void init() {
        InputStream in = Global.class.getClassLoader().getResourceAsStream(APPLICATION_CONFIG);
        PropertiesUtil util = new PropertiesUtil(in);
        tempMap.putAll(util.getValues());

    }


    /**
     * 获取配置
     */
    public static String getString(String key) {

        String value = null;
        if (tempMap.containsKey(key)) {
            value = tempMap.get(key);
        }
        return value;
    }

    public static int getInt(String key) {
        int value = 0;
        if (tempMap.containsKey(key)) {
            value = Integer.parseInt(tempMap.get(key));
        }
        return value;
    }

    public static void getMapByPrefix(String prefix, Map<String, String> returnMap) {
        int length = prefix.length() + 1;
        for (Map.Entry<String, String> entry : tempMap.entrySet()) {
            if (entry.getKey().startsWith(prefix) && ValidateUtil.isValid(entry.getValue())) {
                returnMap.put(entry.getKey().substring(length), entry.getValue());
            }
        }
    }

    /**
     * 吧获取的key返回list
     *
     * @param prefix
     * @return
     */
    public static List<String> convertToListByKey(String prefix) {
        List<String> list = new ArrayList<>();
        Map<String, String> values = new HashMap<>();
        Global.getMapByPrefix(prefix, values);
        for (Map.Entry<String, String> entry : values.entrySet()) {
            list.add(entry.getKey());
        }
        return list;
    }

    /**
     * 吧获取的key返回list
     *
     * @param prefix
     * @return
     */
    public static List<String> convertToListByValue(String prefix) {
        List<String> list = new ArrayList<>();
        Map<String, String> values = new HashMap<>();
        Global.getMapByPrefix(prefix, values);
        for (Map.Entry<String, String> entry : values.entrySet()) {
            list.add(entry.getValue());
        }
        return list;
    }

    public static Map<String, String> getMapByPrefix(String prefix) {
        Map<String, String> getMap = new HashMap<>();
        int length = prefix.length() + 1;
        for (Map.Entry<String, String> entry : tempMap.entrySet()) {
            if (entry.getKey().startsWith(prefix + ".")) {
                getMap.put(entry.getKey().substring(length), entry.getValue());
            }
        }
        return getMap;
    }

    public static String getValue(String prefix, String key) {
        String name = prefix + key;
        if (tempMap.containsKey(name)) {
            return tempMap.get(name);
        }
        return null;
    }

    public static void reset() {
        tempMap.clear();
        init();
    }
}
